<?php

Globals::requireClass('Controller');
Globals::requireTable('Order');
Globals::requireTable('Orderdetail');
Globals::requireTable('Ordermoney');
Globals::requireTable('Custom_site');
Globals::requireTable('Custom_site_contact');
Globals::requireTable('Custom');
Globals::requireTable('Custom_contact');
Globals::requireTable('Servicemode');
Globals::requireTable('DeliveryPoint');
Globals::requireTable('City');
Globals::requireTable('Box');
Globals::requireTable('Yard');
Globals::requireTable('Car');
Globals::requireTable('Driver');
Globals::requireTable('Carteam');
Globals::requireTable('Delivery');

class OrderController extends Controller {

    protected $OrderTable;
    protected $CustomTable;
    protected $Custom_siteTable;
    protected $ServiceModeTable;
    protected $DeliveryPointTable;
    protected $CityTable;
    protected $BoxTable;
    protected $YardTable;
    protected $CarTable;
    protected $DriverTable;
    protected $CarteamTable;
    protected $DeliveryTable;
    protected $OrderdetailTable;
    protected $OrdermoneyTable;
    protected $Custom_contactTable;
    protected $Custom_site_contactTable;
    public static $defaultConfig = array(
        'viewEnabled' => true,
        'layoutEnabled' => true,
        'title' => null
    );

    public function __construct($config = null) {
        parent::__construct($config);
        $this->OrderTable = new OrderTable($config);
        $this->CustomTable = new CustomTable($config);
        $this->Custom_siteTable = new Custom_siteTable($config);
        $this->ServiceModeTable = new ServiceModeTable($config);
        $this->DeliveryPointTable = new DeliveryPointTable($config);
        $this->CityTable = new CityTable($config);
        $this->BoxTable = new BoxTable($config);
        $this->YardTable = new YardTable($config);
        $this->CarTable = new CarTable($config);
        $this->DriverTable = new DriverTable($config);
        $this->CarteamTable = new CarteamTable($config);
        $this->DeliveryTable = new DeliveryTable($config);
        $this->OrderdetailTable = new OrderdetailTable($config);
        $this->OrdermoneyTable = new OrdermoneyTable($config);
        $this->Custom_contactTable = new Custom_contactTable($config);
        $this->Custom_site_contactTable = new Custom_site_contactTable($config);
    }

    public function indexAction() {
        
    }

    public function listAction() {
        $orderid = $this->getParam('search_orderid');
        $custom_id = $this->getIntParam('custom_id'); //客户简称
        $lading_code = $this->getParam('lading_code'); //提单号
        $orderid_detail = $this->getParam('orderid_detail');
        $team_id = $this->getIntParam("team_id");
        $car_id = $this->getIntParam("car_id");
        $box_code = $this->getParam("box_code");
        $start = $this->getParam("start");
        $end = $this->getParam("end");
        $where[] = "status > 0";
        if(!empty($custom_id)){
         $where[] = "`custom_id` = $custom_id";
         $this->view->custom_id = $custom_id;
        }
        if(!empty($lading_code)){
        $where[] = "`lading_code` like '%{$lading_code}%'";
        $this->view->lading_code = $lading_code;
        }
        $custom = $this->CustomTable->listAll("`status` = 1");
//        $team = $this->
        $this->view->custom = isset($custom) ? $custom : array();
        
        $order = $this->OrderTable->listall($where);
        foreach($order as $k=>$v){
            $orderid_ids[] = $v['orderid'];
            $service_ids[] = $v['service_mode_id'];
        }
        $service_ids = @array_unique($service_ids);
        $service_ar = @implode(",",$service_ids);
        if($service_ar){
        $service = $this->ServiceModeTable->listAll("`id` in ({$service_ar})");   
        }
        foreach($service as $k=>$v){
        $service_data[$v['id']] = $v['servicemode'];    
        }
        $this->view->service = isset($service_data) ? $service_data : array();
        $orderid_ids = @array_unique($orderid_ids);
        $orderid_ar =  @implode(",",$orderid_ids);
        if(!empty($orderid_ar)){
          $xwhere[] = "`orderid` in ({$orderid_ar})";
        }else{
           $xwhere[] = "`orderid` = '就是应该找不到'";
        }
        if(!empty($orderid_detail)){
          $xwhere[] = "`orderid_detail` like '%{$orderid_detail}%'"; 
          $this->view->orderid_detail = $orderid_detail;
        }
        if(!empty($team_id)){
          $xwhere[] = "`car_team_id` = $team_id";
          $this->view->team_id = $team_id;
        }
        if(!empty($car_id)){
          $xwhere[] = "`car_id` = $car_id";
          $this->view->car_id = $car_id;
        }
        if(!empty($box_code)){
          $xwhere[] = "`box_code` like '%{$box_code}%'";  
          $this->view->box_code = $box_code;
        }
        if (!empty($start)) {
            $xwhere[] = "`dobox_date` >= '{$start}'";
            $this->view->start = $start;
        }
        if (!empty($end)) {
            $xwhere[] = "`dobox_date` <= '{$end}'";
            $this->view->end = $end;
        }
        $car = $this->CarTable->listAll("`status` = 1");
        $this->view->car = isset($car) ? $car : array();
        $team = $this->CarteamTable->listAll("`status` = 1");
        $this->view->team = isset($team) ? $team : array(); 
        $custom = $this->CustomTable->listall("user_id = 1 and status > 0");
        $custom_site = $this->Custom_siteTable->listall("user_id = 1 and status > 0");
        $yard = $this->YardTable->listall("user_id = 1 and status > 0");
        $car = $this->CarTable->listall("user_id = 1 and status > 0");
        $driver = $this->DriverTable->listall("user_id = 1 and status > 0");
        $carteam = $this->CarteamTable->listall("user_id = 1 and status > 0");

        /********************/
        $xwhere[] = "user_id = 1 and status > 0 AND status < 3";
        $detail = $this->OrderdetailTable->listall($xwhere);



        foreach ($detail as $k => $v) {
            $detail[$k]['custom_id'] = '';
            $detail[$k]['custom_site_id'] = '';
            $detail[$k]['ship_name'] = '';
            $detail[$k]['ship_line'] = '';
            $detail[$k]['lading_code'] = '';
            $detail[$k]['service_mode_id'] = '';
            $detail[$k]['send_yard_id'] = '';
            $detail[$k]['back_yard_id'] = '';
            $detail[$k]['inner_code'] = '';
            $detail[$k]['transfer_time'] = '';
            foreach ($order as $kk => $vv) {
                if ($v['orderid'] == $vv['orderid']) {
                    $detail[$k]['custom_id'] = $order[$kk]['custom_id'];
                    $detail[$k]['custom_site_id'] = $order[$kk]['custom_site_id'];
                    $detail[$k]['ship_name'] = $order[$kk]['ship_name'];
                    $detail[$k]['ship_line'] = $order[$kk]['ship_line'];
                    $detail[$k]['lading_code'] = $order[$kk]['lading_code'];
                    $detail[$k]['service_mode_id'] = $order[$kk]['service_mode_id'];
                    $detail[$k]['send_yard_id'] = $order[$kk]['send_yard_id'];
                    $detail[$k]['back_yard_id'] = $order[$kk]['back_yard_id'];
                    $detail[$k]['inner_code'] = $order[$kk]['inner_code'];
                    $detail[$k]['transfer_time'] = $order[$kk]['transfer_time'];
                }
            }
        }

        foreach ($detail as $k => $v) {
            foreach ($custom as $kk => $vv) {
                if ($v['custom_id'] == $vv['id']) {
                    $detail[$k]['custom_id'] = $vv['nickname'];
                }
            }
        }

        foreach ($detail as $k => $v) {
            foreach ($custom_site as $kk => $vv) {
                if ($v['custom_site_id'] == $vv['id']) {
                    $detail[$k]['custom_site_id'] = $vv['name'];
                }
            }
        }

        foreach ($detail as $k => $v) {
            foreach ($yard as $kk => $vv) {
                if ($v['send_yard_id'] == $vv['id']) {
                    $detail[$k]['send_yard_id'] = $vv['name'];
                }
                if ($v['back_yard_id'] == $vv['id']) {
                    $detail[$k]['back_yard_id'] = $vv['name'];
                }
            }
        }

        foreach ($detail as $k => $v) {
            foreach ($car as $kk => $vv) {
                if ($v['car_id'] == $vv['id']) {
                    $detail[$k]['car_id'] = $vv['name'];
                }
            }
        }

        foreach ($detail as $k => $v) {
            foreach ($driver as $kk => $vv) {
                if ($v['driver_id'] == $vv['id']) {
                    $detail[$k]['driver_id'] = $vv['name'];
                }
            }
        }

        foreach ($detail as $k => $v) {
            foreach ($carteam as $kk => $vv) {
                if ($v['car_team_id'] == $vv['id']) {
                    $detail[$k]['car_team_id'] = $vv['name'];
                }
            }
        }
        $this->view->order = $order;
        $this->view->detail = $detail;
        $this->config['layoutEnabled'] = false;
        $type = $this->getIntParam("type");
        
    }

    public function detailAction() {
        $id = $this->getIntParam('id');
        if ($id > 0) {
            $order = $this->OrderTable->getRow($id);
            $this->view->order = $order;
        }

        $custom = $this->CustomTable->listAll(array('status' => 1, 'user_id' => $_SESSION['userinfo']['id']));
        $this->view->custom = $custom;

        $serivce_model = $this->ServiceModeTable->listAll(array('status' => 1, 'user_id' => $_SESSION['userinfo']['id']));
        $this->view->serivce_model = $serivce_model;

        $delivery_point = $this->DeliveryPointTable->listAll(array('status' => 1, 'user_id' => $_SESSION['userinfo']['id']));
        $this->view->delivery_point = $delivery_point;

        $city = $this->CityTable->listAll(array('status' => 1, 'user_id' => $_SESSION['userinfo']['id']));
        $this->view->city = $city;

        $box = $this->BoxTable->listAll(array('status' => 1, 'user_id' => $_SESSION['userinfo']['id']));
        $this->view->box = $box;

        $yard = $this->YardTable->listAll(array('status' => 1, 'user_id' => $_SESSION['userinfo']['id']));
        $this->view->yard = $yard;

        $car = $this->CarTable->listAll(array('status' => 1, 'user_id' => $_SESSION['userinfo']['id']));
        $this->view->car = $car;

        $driver = $this->DriverTable->listAll(array('status' => 1, 'user_id' => $_SESSION['userinfo']['id']));
        $this->view->driver = $driver;

        $carteam = $this->CarteamTable->listAll(array('status' => 1, 'user_id' => $_SESSION['userinfo']['id']));
        $this->view->carteam = $carteam;

        $delivery = $this->DeliveryTable->listAll(array('status' => 1, 'user_id' => $_SESSION['userinfo']['id']));
        $this->view->delivery = $delivery;

        $this->config['layoutEnabled'] = false;
    }

    public function dodetailAction() {
        $text_name = date("Ymd", time());
        $fp = @fopen('./orderid/' . $text_name . '.txt', 'r');
        $count = (int) @fgets($fp, 10);
        @fclose($fp);
        $count++;
        $fp = fopen('./orderid/' . $text_name . '.txt', 'w');
        fputs($fp, $count, strlen($count));
        fclose($fp);
        $text_count = 10000 + $count;
        $text_count = substr($text_count, -4);
        $orderid = substr($text_name, -6) . $text_count;

        $custom_id = $this->getParam('custom_id');
        $custom_contact_id = $this->getParam('custom_contact_id');
        $custom_code = $this->getParam('custom_code');
        $service_mode_id = $this->getParam('service_mode_id');
        $custom_site_id = $this->getParam('custom_site_id');
        $custom_site_contact_id = $this->getParam('custom_site_contact_id');
        $contact_phone = $this->getParam('contact_phone');
        $goods_city = $this->getParam('goods_city');
        $goods_address = $this->getParam('goods_address');
        $danger_level = $this->getParam('danger_level');
        $nation_code = $this->getParam('nation_code');
        $lading_code = $this->getParam('lading_code');
        $ship_name = $this->getParam('ship_name');
        $ship_line = $this->getParam('ship_line');
        $ship_company = $this->getParam('ship_company');
        $transfer_time = $this->getParam('transfer_time');
        $ship_date = $this->getParam('ship_date');
        $goods_port = $this->getParam('goods_port');
        $delivery = $this->getParam('delivery');
        $start_time = $this->getParam('start_time');
        $end_time = $this->getParam('end_time');
        $send_yard_id = $this->getParam('send_yard_id');
        $back_yard_id = $this->getParam('back_yard_id');
        $port_name = $this->getParam('port_name');
        $in_time = $this->getParam('in_time');
        $out_code = $this->getParam('out_code');
        $inner_code = $this->getParam('inner_code');

        $is_double = $this->getParam('is_double');
        $box_id = $this->getParam('box_id');
        $box_code = $this->getParam('box_code');
        $seal_code = $this->getParam('seal_code');
        $dobox_date = $this->getParam('dobox_date');
        $dobox_time = $this->getParam('dobox_time');
        $goods_count = $this->getParam('goods_count');
        $goods_weight = $this->getParam('goods_weight');
        $goods_size = $this->getParam('goods_size');
        $goods_name = $this->getParam('goods_name');
        $car_id = $this->getParam('car_id');
        $driver_id = $this->getParam('driver_id');
        $car_team_id = $this->getParam('car_team_id');
        $driver_phone = $this->getParam('driver_phone');
        $delivery_id = $this->getParam('delivery_id');
        $arrive_time = $this->getParam('arrive_time');
        $kilometer = $this->getParam('kilometer');
        $weight = $this->getParam('weight');
        $remark_box = $this->getParam('remark_box');
        $remark_big = $this->getParam('remark_big');

        $money_1_1 = $this->getParam('money_1_1');
        $money_1_2 = $this->getParam('money_1_2');
        $money_1_3 = $this->getParam('money_1_3');
        $money_1_4 = $this->getParam('money_1_4');
        $money_1_5 = $this->getParam('money_1_5');
        $money_1_6 = $this->getParam('money_1_6');
        $money_1_7 = $this->getParam('money_1_7');
        $money_1_8 = $this->getParam('money_1_8');
        $money_1_9 = $this->getParam('money_1_9');
        $money_1_10 = $this->getParam('money_1_10');
        $money_1_11 = $this->getParam('money_1_11');
        $money_1_12 = $this->getParam('money_1_12');
        $money_1_13 = $this->getParam('money_1_13');
        $money_1_14 = $this->getParam('money_1_14');
        $money_1_15 = $this->getParam('money_1_15');
        $remark_1 = $this->getParam('remark_1');

        $money_2_1 = $this->getParam('money_2_1');
        $money_2_2 = $this->getParam('money_2_2');
        $money_2_3 = $this->getParam('money_2_3');
        $money_2_4 = $this->getParam('money_2_4');
        $money_2_5 = $this->getParam('money_2_5');
        $money_2_6 = $this->getParam('money_2_6');
        $money_2_7 = $this->getParam('money_2_7');
        $money_2_8 = $this->getParam('money_2_8');
        $money_2_9 = $this->getParam('money_2_9');
        $money_2_10 = $this->getParam('money_2_10');
        $money_2_11 = $this->getParam('money_2_11');
        $money_2_12 = $this->getParam('money_2_12');
        $money_2_13 = $this->getParam('money_2_13');
        $money_2_14 = $this->getParam('money_2_14');
        $money_2_15 = $this->getParam('money_2_15');
        $remark_2 = $this->getParam('remark_2');

        $money_3_1 = $this->getParam('money_3_1');
        $money_3_2 = $this->getParam('money_3_2');
        $money_3_3 = $this->getParam('money_3_3');
        $money_3_4 = $this->getParam('money_3_4');
        $money_3_5 = $this->getParam('money_3_5');
        $money_3_6 = $this->getParam('money_3_6');
        $money_3_7 = $this->getParam('money_3_7');
        $money_3_8 = $this->getParam('money_3_8');
        $money_3_9 = $this->getParam('money_3_9');
        $money_3_10 = $this->getParam('money_3_10');
        $money_3_11 = $this->getParam('money_3_11');
        $money_3_12 = $this->getParam('money_3_12');
        $money_3_13 = $this->getParam('money_3_13');
        $money_3_14 = $this->getParam('money_3_14');
        $money_3_15 = $this->getParam('money_3_15');
        $remark_3 = $this->getParam('remark_3');
        $order_money_1 = $money_1_1 + $money_1_2 + $money_1_3 + $money_1_4 + $money_1_5 + $money_1_6 + $money_1_7 + $money_1_8 + $money_1_9 + $money_1_10 + $money_1_11 + $money_1_12 + $money_1_13 + $money_1_14 + $money_1_15;
        $order_money_2 = $money_2_1 + $money_2_2 + $money_2_3 + $money_2_4 + $money_2_5 + $money_2_6 + $money_2_7 + $money_2_8 + $money_2_9 + $money_2_10 + $money_2_11 + $money_2_12 + $money_2_13 + $money_2_14 + $money_2_15;
        $order_money_3 = $money_3_1 + $money_3_2 + $money_3_3 + $money_3_4 + $money_3_5 + $money_3_6 + $money_3_7 + $money_3_8 + $money_3_9 + $money_3_10 + $money_3_11 + $money_3_12 + $money_3_13 + $money_3_14 + $money_3_15;
        $detail_count = $this->getIntParam('detail_count');
        $detail_count = $detail_count < 1 ? 1 : $detail_count;

        $time = date("Y-m-d H:i:s", time());

        $order_data = array(
            'user_id' => $_SESSION['userinfo']['id'],
            'orderid' => $orderid,
            'custom_id' => $custom_id,
            'custom_contact_id' => $custom_contact_id,
            'custom_code' => $custom_code,
            'service_mode_id' => $service_mode_id,
            'custom_site_id' => $custom_site_id,
            'custom_site_contact_id' => $custom_site_contact_id,
            'contact_phone' => $contact_phone,
            'goods_city' => $goods_city,
            'goods_address' => $goods_address,
            'danger_level' => $danger_level,
            'nation_code' => $nation_code,
            'lading_code' => $lading_code,
            'ship_name' => $ship_name,
            'ship_line' => $ship_line,
            'ship_company' => $ship_company,
            'transfer_time' => $transfer_time,
            'ship_date' => $ship_date,
            'goods_port' => $goods_port,
            'delivery' => $delivery,
            'start_time' => $start_time,
            'end_time' => $end_time,
            'send_yard_id' => $send_yard_id,
            'back_yard_id' => $back_yard_id,
            'port_name' => $port_name,
            'in_time' => $in_time,
            'out_code' => $out_code,
            'inner_code' => $inner_code,
            'createtime' => $time,
            'count' => $detail_count,
            'money_1' => $order_money_1,
            'money_2' => $order_money_2,
            'money_3' => $order_money_3,
        );


        $this->OrderTable->start();

        try {
            $order_id = $this->OrderTable->add($order_data, true);
            for ($i = 1; $i <= $detail_count; $i++) {
                $detail_data = array(
                    'user_id' => $_SESSION['userinfo']['id'],
                    'orderid' => $orderid,
                    'orderid_detail' => $orderid . '-' . $i,
                    'is_double' => $is_double,
                    'box_id' => $box_id,
                    'box_code' => $box_code,
                    'seal_code' => $seal_code,
                    'dobox_date' => $dobox_date,
                    'dobox_time' => $dobox_time,
                    'goods_count' => $goods_count,
                    'goods_weight' => $goods_weight,
                    'goods_size' => $goods_size,
                    'goods_name' => $goods_name,
                    'car_id' => $car_id,
                    'driver_id' => $driver_id,
                    'car_team_id' => $car_team_id,
                    'driver_phone' => $driver_phone,
                    'delivery_id' => $delivery_id,
                    'arrive_time' => $arrive_time,
                    'kilometer' => $kilometer,
                    'weight' => $weight,
                    'remark_box' => $remark_box,
                    'remark_big' => $remark_big,
                    'createtime' => $time,
                    'update_user' => $_SESSION['userinfo']['username'],
                    'update_time' => $time,
                );


                $detail_id = $this->OrderdetailTable->add($detail_data, true);
                if ($detail_id > 0) {
                    $money_1 = array(
                        'user_id' => $_SESSION['userinfo']['id'],
                        'orderid' => $orderid,
                        'detail_id' => $detail_id,
                        'money_1' => $money_1_1,
                        'money_2' => $money_1_2,
                        'money_3' => $money_1_3,
                        'money_4' => $money_1_4,
                        'money_5' => $money_1_5,
                        'money_6' => $money_1_6,
                        'money_7' => $money_1_7,
                        'money_8' => $money_1_8,
                        'money_9' => $money_1_9,
                        'money_10' => $money_1_10,
                        'money_11' => $money_1_11,
                        'money_12' => $money_1_12,
                        'money_13' => $money_1_13,
                        'money_14' => $money_1_14,
                        'money_15' => $money_1_15,
                        'money_remark' => $remark_1,
                        'type' => 1,
                        'createtime' => $time,
                    );

                    $money_2 = array(
                        'user_id' => $_SESSION['userinfo']['id'],
                        'orderid' => $orderid,
                        'detail_id' => $detail_id,
                        'money_1' => $money_2_1,
                        'money_2' => $money_2_2,
                        'money_3' => $money_2_3,
                        'money_4' => $money_2_4,
                        'money_5' => $money_2_5,
                        'money_6' => $money_2_6,
                        'money_7' => $money_2_7,
                        'money_8' => $money_2_8,
                        'money_9' => $money_2_9,
                        'money_10' => $money_2_10,
                        'money_11' => $money_2_11,
                        'money_12' => $money_2_12,
                        'money_13' => $money_2_13,
                        'money_14' => $money_2_14,
                        'money_15' => $money_2_15,
                        'money_remark' => $remark_2,
                        'type' => 2,
                        'createtime' => $time,
                    );

                    $money_3 = array(
                        'user_id' => $_SESSION['userinfo']['id'],
                        'orderid' => $orderid,
                        'detail_id' => $detail_id,
                        'money_1' => $money_3_1,
                        'money_2' => $money_3_2,
                        'money_3' => $money_3_3,
                        'money_4' => $money_3_4,
                        'money_5' => $money_3_5,
                        'money_6' => $money_3_6,
                        'money_7' => $money_3_7,
                        'money_8' => $money_3_8,
                        'money_9' => $money_3_9,
                        'money_10' => $money_3_10,
                        'money_11' => $money_3_11,
                        'money_12' => $money_3_12,
                        'money_13' => $money_3_13,
                        'money_14' => $money_3_14,
                        'money_15' => $money_3_15,
                        'money_remark' => $remark_3,
                        'type' => 3,
                        'createtime' => $time,
                    );
                    $money_val_1 = $this->OrdermoneyTable->add($money_1);
                    $money_val_2 = $this->OrdermoneyTable->add($money_2);
                    $money_val_3 = $this->OrdermoneyTable->add($money_3);
                }
            }


            echo 1;
        } catch (Exception $ex) {
            $this->OrderTable->back();
            echo $ex;
        }

        $this->OrderTable->COMMIT();


        $this->config['layoutEnabled'] = false;
        exit;
    }

    public function updateAction() {
        $orderid_detail = $this->getParam('orderid_detail');

        $orderid = substr($orderid_detail, 0, 10);
        $order_num = substr($orderid_detail, 11);
        $this->view->orderid_detail = $orderid_detail ? $orderid_detail : 0;
        $this->view->order_num = $order_num ? $order_num : 0;
        $this->view->orderid = $orderid;
        $where = "orderid = '{$orderid}' AND  `status` > 0 AND `status` < 3";

        $order_data = $this->OrderTable->listAll($where);

        $orderdetail_data = $this->OrderdetailTable->listAll($where);

        $ordermoney_data = $this->OrdermoneyTable->listAll($where);

        $custom = $this->CustomTable->listAll(array('status' => 1, 'user_id' => $_SESSION['userinfo']['id']));
        $this->view->custom = $custom;

        $Custom_contact = $this->Custom_contactTable->listAll(array('status' => 1, 'user_id' => $_SESSION['userinfo']['id']));
        $this->view->Custom_contact = $Custom_contact;

        $custom_site = $this->Custom_siteTable->listAll(array('status' => 1, 'user_id' => $_SESSION['userinfo']['id']));
        $this->view->custom_site = $custom_site;

        $custom_site_contact = $this->Custom_site_contactTable->listAll(array('status' => 1, 'user_id' => $_SESSION['userinfo']['id']));
        $this->view->custom_site_contact = $custom_site_contact;

        $serivce_model = $this->ServiceModeTable->listAll(array('status' => 1, 'user_id' => $_SESSION['userinfo']['id']));
        $this->view->serivce_model = $serivce_model;

        $delivery_point = $this->DeliveryPointTable->listAll(array('status' => 1, 'user_id' => $_SESSION['userinfo']['id']));
        $this->view->delivery_point = $delivery_point;

        $city = $this->CityTable->listAll(array('status' => 1, 'user_id' => $_SESSION['userinfo']['id']));
        $this->view->city = $city;

        $box = $this->BoxTable->listAll(array('status' => 1, 'user_id' => $_SESSION['userinfo']['id']));
        $this->view->box = $box;

        $yard = $this->YardTable->listAll(array('status' => 1, 'user_id' => $_SESSION['userinfo']['id']));
        $this->view->yard = $yard;

        $car = $this->CarTable->listAll(array('status' => 1, 'user_id' => $_SESSION['userinfo']['id']));
        $this->view->car = $car;

        $driver = $this->DriverTable->listAll(array('status' => 1, 'user_id' => $_SESSION['userinfo']['id']));
        $this->view->driver = $driver;

        $carteam = $this->CarteamTable->listAll(array('status' => 1, 'user_id' => $_SESSION['userinfo']['id']));
        $this->view->carteam = $carteam;

        $delivery = $this->DeliveryTable->listAll(array('status' => 1, 'user_id' => $_SESSION['userinfo']['id']));
        $this->view->delivery = $delivery;


        foreach ($orderdetail_data as $k => $v) {
            $aa = 0;
            $orderdetail_data[$k]['order'] = $order_data;
            foreach ($ordermoney_data as $kk => $vv) {
                if ($v['id'] == $vv['detail_id']) {
                    $orderdetail_data[$k]['money'][$aa] = $ordermoney_data[$kk];
                    $aa++;
                }
            }
        }
        $this->view->orderdetail_id = $orderdetail_data;

        foreach ($orderdetail_data as $k => $v) {
            if ($v['orderid_detail'] != $orderid_detail) {
                unset($orderdetail_data[$k]);
            }
        }
        $this->view->order_data = $order_data;
        $this->view->orderdetail_data = $orderdetail_data;
        $this->config['layoutEnabled'] = false;
    }

    public function doupdateAction() {
        $orderid = $this->getParam('orderid');
        $orderid_detail = $this->getParam('orderid_detail');
        $detail_id = $this->getParam('detail_id');

        $custom_id = $this->getParam('custom_id');
        $custom_contact_id = $this->getParam('custom_contact_id');
        $custom_code = $this->getParam('custom_code');
        $service_mode_id = $this->getParam('service_mode_id');
        $custom_site_id = $this->getParam('custom_site_id');
        $custom_site_contact_id = $this->getParam('custom_site_contact_id');
        $contact_phone = $this->getParam('contact_phone');
        $goods_city = $this->getParam('goods_city');
        $goods_address = $this->getParam('goods_address');
        $danger_level = $this->getParam('danger_level');
        $nation_code = $this->getParam('nation_code');
        $lading_code = $this->getParam('lading_code');
        $ship_name = $this->getParam('ship_name');
        $ship_line = $this->getParam('ship_line');
        $ship_company = $this->getParam('ship_company');
        $transfer_time = $this->getParam('transfer_time');
        $ship_date = $this->getParam('ship_date');
        $goods_port = $this->getParam('goods_port');
        $delivery = $this->getParam('delivery');
        $start_time = $this->getParam('start_time');
        $end_time = $this->getParam('end_time');
        $send_yard_id = $this->getParam('send_yard_id');
        $back_yard_id = $this->getParam('back_yard_id');
        $port_name = $this->getParam('port_name');
        $in_time = $this->getParam('in_time');
        $out_code = $this->getParam('out_code');
        $inner_code = $this->getParam('inner_code');

        $is_double = $this->getParam('is_double');
        $box_id = $this->getParam('box_id');
        $box_code = $this->getParam('box_code');
        $seal_code = $this->getParam('seal_code');
        $dobox_date = $this->getParam('dobox_date');
        $dobox_time = $this->getParam('dobox_time');
        $goods_count = $this->getParam('goods_count');
        $goods_weight = $this->getParam('goods_weight');
        $goods_size = $this->getParam('goods_size');
        $goods_name = $this->getParam('goods_name');
        $car_id = $this->getParam('car_id');
        $driver_id = $this->getParam('driver_id');
        $car_team_id = $this->getParam('car_team_id');
        $driver_phone = $this->getParam('driver_phone');
        $delivery_id = $this->getParam('delivery_id');
        $arrive_time = $this->getParam('arrive_time');
        $kilometer = $this->getParam('kilometer');
        $weight = $this->getParam('weight');
        $remark_box = $this->getParam('remark_box');
        $remark_big = $this->getParam('remark_big');

        $money_1_1 = $this->getParam('money_1_1');
        $money_1_2 = $this->getParam('money_1_2');
        $money_1_3 = $this->getParam('money_1_3');
        $money_1_4 = $this->getParam('money_1_4');
        $money_1_5 = $this->getParam('money_1_5');
        $money_1_6 = $this->getParam('money_1_6');
        $money_1_7 = $this->getParam('money_1_7');
        $money_1_8 = $this->getParam('money_1_8');
        $money_1_9 = $this->getParam('money_1_9');
        $money_1_10 = $this->getParam('money_1_10');
        $money_1_11 = $this->getParam('money_1_11');
        $money_1_12 = $this->getParam('money_1_12');
        $money_1_13 = $this->getParam('money_1_13');
        $money_1_14 = $this->getParam('money_1_14');
        $money_1_15 = $this->getParam('money_1_15');
        $remark_1 = $this->getParam('remark_1');

        $money_2_1 = $this->getParam('money_2_1');
        $money_2_2 = $this->getParam('money_2_2');
        $money_2_3 = $this->getParam('money_2_3');
        $money_2_4 = $this->getParam('money_2_4');
        $money_2_5 = $this->getParam('money_2_5');
        $money_2_6 = $this->getParam('money_2_6');
        $money_2_7 = $this->getParam('money_2_7');
        $money_2_8 = $this->getParam('money_2_8');
        $money_2_9 = $this->getParam('money_2_9');
        $money_2_10 = $this->getParam('money_2_10');
        $money_2_11 = $this->getParam('money_2_11');
        $money_2_12 = $this->getParam('money_2_12');
        $money_2_13 = $this->getParam('money_2_13');
        $money_2_14 = $this->getParam('money_2_14');
        $money_2_15 = $this->getParam('money_2_15');
        $remark_2 = $this->getParam('remark_2');

        $money_3_1 = $this->getParam('money_3_1');
        $money_3_2 = $this->getParam('money_3_2');
        $money_3_3 = $this->getParam('money_3_3');
        $money_3_4 = $this->getParam('money_3_4');
        $money_3_5 = $this->getParam('money_3_5');
        $money_3_6 = $this->getParam('money_3_6');
        $money_3_7 = $this->getParam('money_3_7');
        $money_3_8 = $this->getParam('money_3_8');
        $money_3_9 = $this->getParam('money_3_9');
        $money_3_10 = $this->getParam('money_3_10');
        $money_3_11 = $this->getParam('money_3_11');
        $money_3_12 = $this->getParam('money_3_12');
        $money_3_13 = $this->getParam('money_3_13');
        $money_3_14 = $this->getParam('money_3_14');
        $money_3_15 = $this->getParam('money_3_15');
        $remark_3 = $this->getParam('remark_3');
        $order_money_1 = $money_1_1 + $money_1_2 + $money_1_3 + $money_1_4 + $money_1_5 + $money_1_6 + $money_1_7 + $money_1_8 + $money_1_9 + $money_1_10 + $money_1_11 + $money_1_12 + $money_1_13 + $money_1_14 + $money_1_15;
        $order_money_2 = $money_2_1 + $money_2_2 + $money_2_3 + $money_2_4 + $money_2_5 + $money_2_6 + $money_2_7 + $money_2_8 + $money_2_9 + $money_2_10 + $money_2_11 + $money_2_12 + $money_2_13 + $money_2_14 + $money_2_15;
        $order_money_3 = $money_3_1 + $money_3_2 + $money_3_3 + $money_3_4 + $money_3_5 + $money_3_6 + $money_3_7 + $money_3_8 + $money_3_9 + $money_3_10 + $money_3_11 + $money_3_12 + $money_3_13 + $money_3_14 + $money_3_15;

        $time = date("Y-m-d H:i:s", time());

        $order_data = array(
            'user_id' => $_SESSION['userinfo']['id'],
            'custom_id' => $custom_id,
            'custom_contact_id' => $custom_contact_id,
            'custom_code' => $custom_code,
            'service_mode_id' => $service_mode_id,
            'custom_site_id' => $custom_site_id,
            'custom_site_contact_id' => $custom_site_contact_id,
            'contact_phone' => $contact_phone,
            'goods_city' => $goods_city,
            'goods_address' => $goods_address,
            'danger_level' => $danger_level,
            'nation_code' => $nation_code,
            'lading_code' => $lading_code,
            'ship_name' => $ship_name,
            'ship_line' => $ship_line,
            'ship_company' => $ship_company,
            'transfer_time' => $transfer_time,
            'ship_date' => $ship_date,
            'goods_port' => $goods_port,
            'delivery' => $delivery,
            'start_time' => $start_time,
            'end_time' => $end_time,
            'send_yard_id' => $send_yard_id,
            'back_yard_id' => $back_yard_id,
            'port_name' => $port_name,
            'in_time' => $in_time,
            'out_code' => $out_code,
            'inner_code' => $inner_code,
            'money_1' => $order_money_1,
            'money_2' => $order_money_2,
            'money_3' => $order_money_3,
        );


        $this->OrderTable->start();

        try {
            $where1 = "orderid = '{$orderid}'";
            $order = $this->OrderTable->update($order_data, $where1);

            $detail_data = array(
                'user_id' => $_SESSION['userinfo']['id'],
                'orderid' => $orderid,
                'is_double' => $is_double,
                'box_id' => $box_id,
                'box_code' => $box_code,
                'seal_code' => $seal_code,
                'dobox_date' => $dobox_date,
                'dobox_time' => $dobox_time,
                'goods_count' => $goods_count,
                'goods_weight' => $goods_weight,
                'goods_size' => $goods_size,
                'goods_name' => $goods_name,
                'car_id' => $car_id,
                'driver_id' => $driver_id,
                'car_team_id' => $car_team_id,
                'driver_phone' => $driver_phone,
                'delivery_id' => $delivery_id,
                'arrive_time' => $arrive_time,
                'kilometer' => $kilometer,
                'weight' => $weight,
                'remark_box' => $remark_box,
                'remark_big' => $remark_big,
                'update_user' => $_SESSION['userinfo']['username'],
                'update_time' => $time,
            );
//                var_dump($detail_data);

            $where2 = "id = '{$detail_id}'";
            $detail = $this->OrderdetailTable->update($detail_data, $where2);

            if ($detail_id > 0) {
                $money_1 = array(
                    'user_id' => $_SESSION['userinfo']['id'],
                    'money_1' => $money_1_1,
                    'money_2' => $money_1_2,
                    'money_3' => $money_1_3,
                    'money_4' => $money_1_4,
                    'money_5' => $money_1_5,
                    'money_6' => $money_1_6,
                    'money_7' => $money_1_7,
                    'money_8' => $money_1_8,
                    'money_9' => $money_1_9,
                    'money_10' => $money_1_10,
                    'money_11' => $money_1_11,
                    'money_12' => $money_1_12,
                    'money_13' => $money_1_13,
                    'money_14' => $money_1_14,
                    'money_15' => $money_1_15,
                    'money_remark' => $remark_1,
                );

                $money_2 = array(
                    'user_id' => $_SESSION['userinfo']['id'],
                    'money_1' => $money_2_1,
                    'money_2' => $money_2_2,
                    'money_3' => $money_2_3,
                    'money_4' => $money_2_4,
                    'money_5' => $money_2_5,
                    'money_6' => $money_2_6,
                    'money_7' => $money_2_7,
                    'money_8' => $money_2_8,
                    'money_9' => $money_2_9,
                    'money_10' => $money_2_10,
                    'money_11' => $money_2_11,
                    'money_12' => $money_2_12,
                    'money_13' => $money_2_13,
                    'money_14' => $money_2_14,
                    'money_15' => $money_2_15,
                    'money_remark' => $remark_2,
                );

                $money_3 = array(
                    'user_id' => $_SESSION['userinfo']['id'],
                    'money_1' => $money_3_1,
                    'money_2' => $money_3_2,
                    'money_3' => $money_3_3,
                    'money_4' => $money_3_4,
                    'money_5' => $money_3_5,
                    'money_6' => $money_3_6,
                    'money_7' => $money_3_7,
                    'money_8' => $money_3_8,
                    'money_9' => $money_3_9,
                    'money_10' => $money_3_10,
                    'money_11' => $money_3_11,
                    'money_12' => $money_3_12,
                    'money_13' => $money_3_13,
                    'money_14' => $money_3_14,
                    'money_15' => $money_3_15,
                    'money_remark' => $remark_3,
                );
                $where3 = "detail_id = '{$detail_id}' and type = 1";
                $where4 = "detail_id = '{$detail_id}' and type = 2";
                $where5 = "detail_id = '{$detail_id}' and type = 3";
                $money_val_1 = $this->OrdermoneyTable->update($money_1, $where3);
                $money_val_2 = $this->OrdermoneyTable->update($money_2, $where4);
                $money_val_3 = $this->OrdermoneyTable->update($money_3, $where5);
            }


            echo 1;
        } catch (Exception $ex) {
            $this->OrderTable->back();
            echo $ex;
        }

        $this->OrderTable->COMMIT();


        $this->config['layoutEnabled'] = false;
        exit;
    }

    public function boxaddAction() {
        $orderid = $this->getParam('orderid');
        $max_count = $this->getIntParam('max_count');
        $num = $max_count + 1;

        $time = date("Y-m-d H:i:s", time());

        $detail_data = array(
            'user_id' => $_SESSION['userinfo']['id'],
            'orderid' => $orderid,
            'orderid_detail' => $orderid . '-' . $num,
            'createtime' => $time,
            'update_user' => $_SESSION['userinfo']['username'],
        );


        $detail_id = $this->OrderdetailTable->add($detail_data, true);

        if ($detail_id > 0) {
            $money_1 = array(
                'user_id' => $_SESSION['userinfo']['id'],
                'orderid' => $orderid,
                'detail_id' => $detail_id,
                'type' => 1,
                'createtime' => $time,
            );

            $money_2 = array(
                'user_id' => $_SESSION['userinfo']['id'],
                'orderid' => $orderid,
                'detail_id' => $detail_id,
                'type' => 2,
                'createtime' => $time,
            );

            $money_3 = array(
                'user_id' => $_SESSION['userinfo']['id'],
                'orderid' => $orderid,
                'detail_id' => $detail_id,
                'type' => 3,
                'createtime' => $time,
            );
            $money_val_1 = $this->OrdermoneyTable->add($money_1);
            $money_val_2 = $this->OrdermoneyTable->add($money_2);
            $money_val_3 = $this->OrdermoneyTable->add($money_3);
        }
        echo 1;
//        orderid_detail
//                $this->redirectToAction('update');
        $this->config['layoutEnabled'] = false;
        exit;
    }

    public function zufeiAction() {
        $id = $this->getParam('id');
        if (empty($id)) {
            echo 'error';
            exit;
        }
        $this->OrderdetailTable->update(array("`status`" => 2), "`id` in ({$id})");
        echo 'sucess';
        exit;
    }

    public function fzufeiAction() {
        $id = $this->getParam('id');
        if (empty($id)) {
            echo 'error';
            exit;
        }
        $this->OrderdetailTable->update(array("`status`" => 1), "`id` in ({$id})");
        echo 'sucess';
        exit;
    }
    
    public function deleteAction(){
        $id = $this->getParam('id');
        if(empty($id)){
            echo 'error';
            exit;
        }
              $this->OrderdetailTable->update(array("`status`" => 0), "`id` in ({$id})");
              echo 'sucess';
              exit;
    }
    
    public function tosubmitAction(){
        $id = $this->getParam('id');
        if(empty($id)){
            echo 'error';
            exit;
        };
        $detail = $this->OrderdetailTable->listAll("`id` in ({$id}) AND `status` = 1");
        $orderid_ids = array();
        foreach($detail as $k=>$v){
             $orderid_ids[] = $v['orderid'];
        }
        $orderid_ids = array_unique($orderid_ids);
        $orderid_ar = implode(",",$orderid_ids);
        $this->OrderdetailTable->update(array("`status`"=>3),"`orderid` in ({$orderid_ar}) AND `status` = 1"); //提交过的订单。
        echo 'sucess';
        exit;
    }
    public function boxprintAction(){

    }
    public function cyprintAction(){
        $id = $this->getParam("id");
        $type = $this->getIntParam("type");
//        switch($type){
//            case '2': 
//                $order = $this->OrderdetailTable->listAll();
//                break;
//            default:
//                 $order = $this->OrderdetailTable->listAll("`id` in ({$id})");
//                break;
//        }
         $order = $this->OrderdetailTable->listAll("`id` in ({$id})");
        foreach($order as $k=>$v){
            $order_ids[] = $v['orderid'];
        }
       $order_ids = array_unique($order_ids);
       $order_ar = implode(",",$order_ids);
       if($order_ar){
        $order_data = $this->OrderTable->listAll("`orderid` in ({$order_ar}) AND `status` = 1");
        $orderdeatil = $this->OrderdetailTable->listAll("`orderid` in ({$order_ar}) AND `status` = 1");
       }
       foreach($order_data as $k=>$v){
        $custom_ids[] = $v['custom_id'];   
       }
       $custom_ids = array_unique($custom_ids);
       $custom_ar = implode(",",$custom_ids);
        if($custom_ar){
         $custom = $this->Custom_contactTable->listAll("`custom_id` in ($custom_ar)");
        }
        foreach($custom as $k=>$v){
            $custom_data[$v['id']] = $v;
        }
       // var_dump($orderdeatil);exit;
        foreach($orderdeatil as $k=>$v){
            $orderdetail_dtat[$v['orderid']][] = $v;
            $driver_ids[] = (int)$v['driver_id'];
            $box_ids[] = (int) $v['box_id'];
        }
       $driver_ids = array_unique($driver_ids);
       $driver_ar = implode(",",$driver_ids);
       $box_ids = array_unique($driver_ids);
       $box_ar = implode(",",$box_ids);
       $box_data = $this->BoxTable->listAll("`id` in ({$box_ar})");
       foreach($box_data as $k=>$v){
           $box_datas[$v['id']] = $v;
       }
       $driver_data = $this->DriverTable->listAll("`id` in ({$driver_ar})");
        foreach($driver_data as $k=>$v){
           $driver_datas[$v['id']] = $v;
        }
        //var_dump($orderdetail_dtat);exit;
        foreach($order_data as $k=>$v){
            $data[$k]['contant'] = $custom_data[$v['custom_id']]['name'];
            $data[$k]['cellphone'] = $custom_data[$v['custom_id']]['cellphone'];
            $data[$k]['telephone'] = $custom_data[$v['custom_id']]['telephone'];
            $data[$k]['ship_name'] = $v['ship_name'];
            $data[$k]['ship_line'] = $v['ship_line'];
            $data[$k]['lading_code'] = $v['lading_code'];
            $data[$k]['address'] = $v['goods_address'];
            $count = 0;
            $weight = 0;
            $driver = array();
            $box_id = array();
            foreach((array)$orderdetail_dtat[$v['orderid']] as $a=>$b){
                $count += $b['goods_count'];
                $weight += $b['goods_weight'];
                if(@$driver_datas[$b['driver_id']]['name']){
                $drivername[] = @$driver_datas[$b['driver_id']]['name'];
                }
               $box_id[] = @$box_datas[(int)$b['box_id']]['code'] ? @$box_datas[(int)$b['box_id']]['code'] : '未知无参数箱子';
            }
               
            $box = array_count_values($box_id);
            $boxname = '';
            $xq = '';
            foreach($box as $c=>$d){
                $boxname .= $xq.$c.'/'.$d;
                $xq = ',';
            }
            $data[$k]['box_name'] = $boxname;
            $drivername = array_unique($drivername);
            $driver_name = implode(',',$drivername);
            $data[$k]['drivername'] = $driver_name;
            $data[$k]['goods_count'] = $count;
            $data[$k]['goods_weight'] = $weight;
        }
        //var_dump($data);
        $this->view->data = isset($data) ? $data : array();
        if($type == 2){
                        error_reporting(E_ALL);
            require_once('./model/PHPExcel.php');
            date_default_timezone_set('PRC');

            $objPHPExcel = new PHPExcel();

            //设置基本属性
            $objPHPExcel->getProperties()->setCreator("lj")
                    ->setLastModifiedBy("lj")
                    ->setTitle("Office 2003 XLSX Test Document")
                    ->setSubject("Office 2003 XLSX Test Document")
                    ->setDescription("Test document for Office 2003 XLSX, generated using PHP classes.")
                    ->setKeywords("office 2003 openxml php")
                    ->setCategory("Test result file");
            //设置工作薄索引
            $objPHPExcel->setActiveSheetIndex(0);

            //设置工作薄名称
            $objAtvSheet = $objPHPExcel->getActiveSheet();
            $objAtvSheet->setTitle("生成报表1");

            //设置标题
            $objAtvSheet->setCellValue('A1', '应付外派车队对账单');
            $objAtvSheet->mergeCells('A1:J1'); //合并单元格
            //$objAtvSheet->mergeCells('A2:X2');
            //设置单元格内容
            $objAtvSheet->setCellValue('A2', '联系人');
            $objAtvSheet->setCellValue('B2', '电话');
            $objAtvSheet->setCellValue('C2', '手机');
            $objAtvSheet->setCellValue('D2', '送货地址');
            $objAtvSheet->setCellValue('E2', '司机');
            $objAtvSheet->setCellValue('F2', '船名');
            $objAtvSheet->setCellValue('G2', '航次');
            $objAtvSheet->setCellValue('H2', '提单号');
            $objAtvSheet->setCellValue('I2', '箱型/数量');
            $objAtvSheet->setCellValue('J2', '件数/净重');

            $count = count($data);
            $RetentionRow = 3;                //设置开始的一行
            //得到最后一行
            $endLine = $RetentionRow + $count;

            //设置表头背景颜色
            $header_Background = new PHPExcel_Style();
            $header_Background->applyFromArray(
                    array(
                        'fill' => array(
                            'type' => PHPExcel_Style_Fill::FILL_SOLID,
                            'color' => array('argb' => 'FF888888')
                        ),
                        'borders' => array(
                            'right' => array('style' => PHPExcel_Style_Border::BORDER_THIN),
                            'bottom' => array('style' => PHPExcel_Style_Border::BORDER_THIN),
                            'top' => array('style' => PHPExcel_Style_Border::BORDER_THIN),
                            'left' => array('style' => PHPExcel_Style_Border::BORDER_THIN)
                        )
                    )
            );
            $objAtvSheet->setSharedStyle($header_Background, "A2:J2");

            //设置内容的边框,注意：这里要先设置边框 在才能设置单元格里的字体颜色
            $content_Border = new PHPExcel_Style();
            $content_Border->applyFromArray(
                    array(
                        'borders' => array(
                            'left' => array('style' => PHPExcel_Style_Border::BORDER_THIN),
                            'top' => array('style' => PHPExcel_Style_Border::BORDER_THIN),
                            'right' => array('style' => PHPExcel_Style_Border::BORDER_THIN),
                            'bottom' => array('style' => PHPExcel_Style_Border::BORDER_THIN)
                        )
                    )
            );
            $objAtvSheet->setSharedStyle($content_Border, "A3:J$endLine");
            //开始填充数据到excel
            $i = 0;
            foreach ($data as $k => $v) {
                $line = $i + $RetentionRow;
                $s1 = $v['contant'];
                $s2 = $v['cellphone'];
                $s3 = $v['telephone'];
                $s4 = $v['address'];
                $s5 = $v['drivername'];
                $s6 = $v['ship_name'];
                $s7 = $v['ship_line'];
                $s8 = $v['lading_code'];
                $s9 = $v['box_name'];
                $s10 = $v['goods_count'].'/'.$v['goods_weight'];
                $objAtvSheet->setCellValue("A$line", $s1);
                $objAtvSheet->setCellValue("B$line", $s2);
                $objAtvSheet->setCellValue("C$line", $s3);
                $objAtvSheet->setCellValue("D$line", $s4);
                $objAtvSheet->setCellValue("E$line", $s5);
                $objAtvSheet->setCellValue("F$line", $s6);
                $objAtvSheet->setCellValue("G$line", $s7);
                $objAtvSheet->setCellValue("H$line", $s8);
                $objAtvSheet->setCellValue("I$line", $s9);
                $objAtvSheet->setCellValue("J$line", $s10);
                $i++;
            }
            //设置 字体加粗，居中
            $objAtvSheet->getStyle('A1:J1')->getFont()->setBold(true);
            $objAtvSheet->getStyle('A1:J1')->getFont()->setSize(20);
            $objAtvSheet->getStyle('A1:J1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
            $objAtvSheet->getStyle('A1:J1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objAtvSheet->getStyle('A1:J1')->getFont()->setName('新宋体');
            $objAtvSheet->getStyle('A1:J1')->getFont()->getColor()->setRGB('E85656');
            $objAtvSheet->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

            //设置内容 居中
            $objAtvSheet->getStyle("A3:J$endLine")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

            //设置单元格宽度
//    $objAtvSheet->getColumnDimension('A')->setWidth(20);
//    $objAtvSheet->getColumnDimension('B')->setWidth(20);
//    $objAtvSheet->getColumnDimension('C')->setWidth(20);
//    $objAtvSheet->getColumnDimension('D')->setWidth(20);
//    $objAtvSheet->getColumnDimension('E')->setWidth(20);
//    $objAtvSheet->getColumnDimension('F')->setWidth(20);
//    $objAtvSheet->getColumnDimension('G')->setWidth(20);
//    $objAtvSheet->getColumnDimension('H')->setWidth(20);
//    $objAtvSheet->getColumnDimension('I')->setWidth(20);
//    $objAtvSheet->getColumnDimension('J')->setWidth(20);
//    $objAtvSheet->getColumnDimension('K')->setWidth(20);
//    $objAtvSheet->getColumnDimension('L')->setWidth(20);
//    $objAtvSheet->getColumnDimension('M')->setWidth(20);
//    $objAtvSheet->getColumnDimension('N')->setWidth(20);
//    $objAtvSheet->getColumnDimension('O')->setWidth(20);
//    $objAtvSheet->getColumnDimension('P')->setWidth(20);
//    $objAtvSheet->getColumnDimension('Q')->setWidth(20);
//    $objAtvSheet->getColumnDimension('R')->setWidth(20);
//    $objAtvSheet->getColumnDimension('S')->setWidth(20);
//    $objAtvSheet->getColumnDimension('T')->setWidth(20);
//    $objAtvSheet->getColumnDimension('U')->setWidth(20);
//    $objAtvSheet->getColumnDimension('V')->setWidth(20);
//    $objAtvSheet->getColumnDimension('W')->setWidth(20);
//    $objAtvSheet->getColumnDimension('X')->setWidth(20);


            $dateString = date('YmdHis') . "应付外派车队对账单.xls";
            $outputFileName = $dateString;
            header('Content-Type: application/vnd.ms-excel');
            header('Content-type: text/csv');
            header('Content-Disposition: attachment;filename="' . $outputFileName . '"');
            header('Cache-Control: max-age=0');

            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
            $objWriter->save('php://output');
        }
    }
    
    public function vipprintAction(){
        $id = $this->getParam("id");
        $order = $this->OrderdetailTable->listAll("`id` in ({$id})");
        foreach($order as $k=>$v){
            $order_ids[] = $v['orderid'];
        }
       $order_ids = array_unique($order_ids);
       $order_ar = implode(",",$order_ids);
       if($order_ar){
        $order_data = $this->OrderTable->listAll("`orderid` in ({$order_ar}) AND `status` = 1");
        $orderdeatil = $this->OrderdetailTable->listAll("`orderid` in ({$order_ar}) AND `status` = 1");
       }
       foreach($order_data as $k=>$v){
        $custom_ids[] = $v['custom_id'];   
       }
       $custom_ids = array_unique($custom_ids);
       $custom_ar = implode(",",$custom_ids);
        if($custom_ar){
         $custom = $this->Custom_contactTable->listAll("`custom_id` in ($custom_ar)");
        }
        foreach($custom as $k=>$v){
            $custom_data[$v['id']] = $v;
        }
       // var_dump($orderdeatil);exit;
        foreach($orderdeatil as $k=>$v){
            $orderdetail_dtat[$v['orderid']][] = $v;
            $driver_ids[] = (int)$v['driver_id'];
            $box_ids[] = (int) $v['box_id'];
            $order_detail_ids[] = $v['id'];
        }
       $order_detail_ar = implode(",",$order_detail_ids);
       $order_money = $this->OrdermoneyTable->listAll("`detail_id` in ({$order_detail_ar}) AND `type` = 1");
    //   var_dump($order_money);exit;
       foreach($order_money as $k=>$v){
        @$order_money_data[$v['orderid']]['money1'] += $v['money_1'];
        @$order_money_data[$v['orderid']]['money2'] += $v['money_2'];
        @$order_money_data[$v['orderid']]['money3'] += $v['money_3'];
        @$order_money_data[$v['orderid']]['money4'] += $v['money_4'];
        @$order_money_data[$v['orderid']]['money5'] += $v['money_5'];
        @$order_money_data[$v['orderid']]['money6'] += $v['money_6'];
        @$order_money_data[$v['orderid']]['money7'] += $v['money_7'];
        @$order_money_data[$v['orderid']]['money8'] += $v['money_8'];
        @$order_money_data[$v['orderid']]['money9'] += $v['money_9'];
        @$order_money_data[$v['orderid']]['money10'] += $v['money_10'];
        @$order_money_data[$v['orderid']]['money11'] += $v['money_11'];
        @$order_money_data[$v['orderid']]['money12'] += $v['money_12'];
        @$order_money_data[$v['orderid']]['money13'] += $v['money_13'];
        @$order_money_data[$v['orderid']]['money14'] += $v['money_14'];
        @$order_money_data[$v['orderid']]['money15'] += $v['money_15'];
       }
       //var_dump($order_money_data);
       $driver_ids = array_unique($driver_ids);
       $driver_ar = implode(",",$driver_ids);
       $box_ids = array_unique($driver_ids);
       $box_ar = implode(",",$box_ids);
       $box_data = $this->BoxTable->listAll("`id` in ({$box_ar})");
       foreach($box_data as $k=>$v){
           $box_datas[$v['id']] = $v;
       }
       $driver_data = $this->DriverTable->listAll("`id` in ({$driver_ar})");
        foreach($driver_data as $k=>$v){
           $driver_datas[$v['id']] = $v;
        }
        //var_dump($orderdetail_dtat);exit;
        foreach($order_data as $k=>$v){
            $data[$k]['contant'] = $custom_data[$v['custom_id']]['name'];
            $data[$k]['cellphone'] = $custom_data[$v['custom_id']]['cellphone'];
            $data[$k]['telephone'] = $custom_data[$v['custom_id']]['telephone'];
            $data[$k]['ship_name'] = $v['ship_name'];
            $data[$k]['ship_line'] = $v['ship_line'];
            $data[$k]['lading_code'] = $v['lading_code'];
            $data[$k]['address'] = $v['goods_address'];
            $data[$k]['money1'] = $order_money_data[$v['orderid']]['money1'];
            $data[$k]['money2'] = $order_money_data[$v['orderid']]['money2'];
            $data[$k]['money3'] = $order_money_data[$v['orderid']]['money3'];
            $data[$k]['money4'] = $order_money_data[$v['orderid']]['money4'];
            $data[$k]['money5'] = $order_money_data[$v['orderid']]['money5'];
            $data[$k]['money6'] = $order_money_data[$v['orderid']]['money6'];
            $data[$k]['money7'] = $order_money_data[$v['orderid']]['money7'];
            $data[$k]['money8'] = $order_money_data[$v['orderid']]['money8'];
            $data[$k]['money9'] = $order_money_data[$v['orderid']]['money9'];
            $data[$k]['money10'] = $order_money_data[$v['orderid']]['money10'];
            $data[$k]['money11'] = $order_money_data[$v['orderid']]['money11'];
            $data[$k]['money12'] = $order_money_data[$v['orderid']]['money12'];
            $data[$k]['money13'] = $order_money_data[$v['orderid']]['money13'];
            $data[$k]['money14'] = $order_money_data[$v['orderid']]['money14'];
            $data[$k]['money15'] = $order_money_data[$v['orderid']]['money15'];
            $data[$k]['custom'] = $custom_data[$v['custom_id']]['name'];
            $data[$k]['custom_id'] = $v['custom_id'];
            $count = 0;
            $weight = 0;
            $driver = array();
            $box_id = array();
            foreach($orderdetail_dtat[$v['orderid']] as $a=>$b){
                $count += $b['goods_count'];
                $weight += $b['goods_weight'];
                if(@$driver_datas[$b['driver_id']]['name']){
                $drivername[] = @$driver_datas[$b['driver_id']]['name'];
                }
               $box_id[] = @$box_datas[(int)$b['box_id']]['code'] ? @$box_datas[(int)$b['box_id']]['code'] : '未知无参数箱子';
            }
               
            $box = array_count_values($box_id);
            $boxname = '';
            $xq = '';
            foreach($box as $c=>$d){
                $boxname .= $xq.$c.'/'.$d;
                $xq = ',';
            }
            $data[$k]['box_name'] = $boxname;
            $drivername = array_unique($drivername);
            $driver_name = implode(',',$drivername);
            $data[$k]['drivername'] = $driver_name;
            $data[$k]['goods_count'] = $count;
            $data[$k]['goods_weight'] = $weight;
        }
        //var_dump($data);
        $this->view->data = isset($data) ? $data : array();
        $type = $this->getIntParam("type");
        if($type == 2){
                                    error_reporting(E_ALL);
            require_once('./model/PHPExcel.php');
            date_default_timezone_set('PRC');

            $objPHPExcel = new PHPExcel();

            //设置基本属性
            $objPHPExcel->getProperties()->setCreator("lj")
                    ->setLastModifiedBy("lj")
                    ->setTitle("Office 2003 XLSX Test Document")
                    ->setSubject("Office 2003 XLSX Test Document")
                    ->setDescription("Test document for Office 2003 XLSX, generated using PHP classes.")
                    ->setKeywords("office 2003 openxml php")
                    ->setCategory("Test result file");
            //设置工作薄索引
            $objPHPExcel->setActiveSheetIndex(0);

            //设置工作薄名称
            $objAtvSheet = $objPHPExcel->getActiveSheet();
            $objAtvSheet->setTitle("生成报表1");

            //设置标题
            $objAtvSheet->setCellValue('A1', '结算单');
            $objAtvSheet->mergeCells('A1:U1'); //合并单元格
            //$objAtvSheet->mergeCells('A2:X2');
            //设置单元格内容
            $objAtvSheet->setCellValue('A2', '客户编号');
            $objAtvSheet->setCellValue('B2', '客户名称');
            $objAtvSheet->setCellValue('C2', '船名/航次');
            $objAtvSheet->setCellValue('D2', '箱型/箱量');
            $objAtvSheet->setCellValue('E2', '提单号');
            $objAtvSheet->setCellValue('F2', '运费');
            $objAtvSheet->setCellValue('G2', '待时费');
            $objAtvSheet->setCellValue('H2', '预提费');
            $objAtvSheet->setCellValue('I2', '洋山费');
            $objAtvSheet->setCellValue('J2', '落/还箱费');
            $objAtvSheet->setCellValue('K2', '打/改单费');
            $objAtvSheet->setCellValue('L2', '理货费');
            $objAtvSheet->setCellValue('M2', '超重费');
            $objAtvSheet->setCellValue('N2', '疏港费');
            $objAtvSheet->setCellValue('O2', '港杂费');
            $objAtvSheet->setCellValue('P2', '坏/污箱费');
            $objAtvSheet->setCellValue('Q2', '其他费');
            $objAtvSheet->setCellValue('R2', '代垫费');
            $objAtvSheet->setCellValue('S2', '税金');
            $objAtvSheet->setCellValue('T2', '代收费');
            $objAtvSheet->setCellValue('U2', '合计:');

            $count = count($data);
            $RetentionRow = 3;                //设置开始的一行
            //得到最后一行
            $endLine = $RetentionRow + $count;

            //设置表头背景颜色
            $header_Background = new PHPExcel_Style();
            $header_Background->applyFromArray(
                    array(
                        'fill' => array(
                            'type' => PHPExcel_Style_Fill::FILL_SOLID,
                            'color' => array('argb' => 'FF888888')
                        ),
                        'borders' => array(
                            'right' => array('style' => PHPExcel_Style_Border::BORDER_THIN),
                            'bottom' => array('style' => PHPExcel_Style_Border::BORDER_THIN),
                            'top' => array('style' => PHPExcel_Style_Border::BORDER_THIN),
                            'left' => array('style' => PHPExcel_Style_Border::BORDER_THIN)
                        )
                    )
            );
            $objAtvSheet->setSharedStyle($header_Background, "A2:U2");

            //设置内容的边框,注意：这里要先设置边框 在才能设置单元格里的字体颜色
            $content_Border = new PHPExcel_Style();
            $content_Border->applyFromArray(
                    array(
                        'borders' => array(
                            'left' => array('style' => PHPExcel_Style_Border::BORDER_THIN),
                            'top' => array('style' => PHPExcel_Style_Border::BORDER_THIN),
                            'right' => array('style' => PHPExcel_Style_Border::BORDER_THIN),
                            'bottom' => array('style' => PHPExcel_Style_Border::BORDER_THIN)
                        )
                    )
            );
            $objAtvSheet->setSharedStyle($content_Border, "A3:U$endLine");
            //开始填充数据到excel
            $i = 0;
            foreach ($data as $k => $v) {
                $line = $i + $RetentionRow;
                $s1 = $v['custom_id'];
                $s2 = $v['custom'];
                $s3 = $v['ship_name'].'/'.$v['ship_line'];
                $s4 = $v['box_name'];
                $s5 = $v['lading_code'];
                $s6 = $v['money1'];
                $s7 = $v['money2'];
                $s8 = $v['money3'];
                $s9 = $v['money4'];
                $s10 = $v['money5'];
                $s11 = $v['money6'];
                $s12 = $v['money7'];
                $s13 = $v['money8'];
                $s14 = $v['money9'];
                $s15 = $v['money10'];
                $s16 = $v['money11'];
                $s17 = $v['money12'];
                $s18 = $v['money13'];
                $s19 = $v['money14'];
                $s20 = $v['money15'];
                $s21 = $v['money1'] +  $v['money2'] +$v['money3'] + $v['money4'] + $v['money5'] + $v['money6'] + $v['money7'] + $v['money8'] + $v['money9'] + $v['money10'] + $v['money11'] + $v['money12']+ $v['money13'] + $v['money14'] + $v['money15'];
                $objAtvSheet->setCellValue("A$line", $s1);
                $objAtvSheet->setCellValue("B$line", $s2);
                $objAtvSheet->setCellValue("C$line", $s3);
                $objAtvSheet->setCellValue("D$line", $s4);
                $objAtvSheet->setCellValue("E$line", $s5);
                $objAtvSheet->setCellValue("F$line", $s6);
                $objAtvSheet->setCellValue("G$line", $s7);
                $objAtvSheet->setCellValue("H$line", $s8);
                $objAtvSheet->setCellValue("I$line", $s9);
                $objAtvSheet->setCellValue("J$line", $s10);
                $objAtvSheet->setCellValue("K$line", $s11);
                $objAtvSheet->setCellValue("L$line", $s12);
                $objAtvSheet->setCellValue("M$line", $s13);
                $objAtvSheet->setCellValue("N$line", $s14);
                $objAtvSheet->setCellValue("O$line", $s15);
                $objAtvSheet->setCellValue("P$line", $s16);
                $objAtvSheet->setCellValue("Q$line", $s17);
                $objAtvSheet->setCellValue("R$line", $s18);
                $objAtvSheet->setCellValue("S$line", $s19);
                $objAtvSheet->setCellValue("T$line", $s20);
                $objAtvSheet->setCellValue("U$line", $s21);
                $i++;
            }
            //设置 字体加粗，居中
            $objAtvSheet->getStyle('A1:U1')->getFont()->setBold(true);
            $objAtvSheet->getStyle('A1:U1')->getFont()->setSize(20);
            $objAtvSheet->getStyle('A1:U1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
            $objAtvSheet->getStyle('A1:U1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objAtvSheet->getStyle('A1:U1')->getFont()->setName('新宋体');
            $objAtvSheet->getStyle('A1:U1')->getFont()->getColor()->setRGB('E85656');
            $objAtvSheet->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

            //设置内容 居中
            $objAtvSheet->getStyle("A3:U$endLine")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
//exit;
            //设置单元格宽度
//    $objAtvSheet->getColumnDimension('A')->setWidth(20);
//    $objAtvSheet->getColumnDimension('B')->setWidth(20);
//    $objAtvSheet->getColumnDimension('C')->setWidth(20);
//    $objAtvSheet->getColumnDimension('D')->setWidth(20);
//    $objAtvSheet->getColumnDimension('E')->setWidth(20);
//    $objAtvSheet->getColumnDimension('F')->setWidth(20);
//    $objAtvSheet->getColumnDimension('G')->setWidth(20);
//    $objAtvSheet->getColumnDimension('H')->setWidth(20);
//    $objAtvSheet->getColumnDimension('I')->setWidth(20);
//    $objAtvSheet->getColumnDimension('J')->setWidth(20);
//    $objAtvSheet->getColumnDimension('K')->setWidth(20);
//    $objAtvSheet->getColumnDimension('L')->setWidth(20);
//    $objAtvSheet->getColumnDimension('M')->setWidth(20);
//    $objAtvSheet->getColumnDimension('N')->setWidth(20);
//    $objAtvSheet->getColumnDimension('O')->setWidth(20);
//    $objAtvSheet->getColumnDimension('P')->setWidth(20);
//    $objAtvSheet->getColumnDimension('Q')->setWidth(20);
//    $objAtvSheet->getColumnDimension('R')->setWidth(20);
//    $objAtvSheet->getColumnDimension('S')->setWidth(20);
//    $objAtvSheet->getColumnDimension('T')->setWidth(20);
//    $objAtvSheet->getColumnDimension('U')->setWidth(20);
//    $objAtvSheet->getColumnDimension('V')->setWidth(20);
//    $objAtvSheet->getColumnDimension('W')->setWidth(20);
//    $objAtvSheet->getColumnDimension('X')->setWidth(20);

            $dateString = date('YmdHis') . "结算单.xls";
            $outputFileName = $dateString;
            header('Content-Type: application/vnd.ms-excel');
            header('Content-type: text/csv');
            header('Content-Disposition: attachment;filename="' . $outputFileName . '"');
            header('Cache-Control: max-age=0');

            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
            $objWriter->save('php://output');
        }
    }
        
    protected function out() {
        $this->layout->nav = 'index';
        parent::out();
    }

}

Config::extend('OrderController', 'Controller');

